﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI;
using System.IO.Compression;
using SFWS.XHandler.Core;
using System.Data;
namespace JKF.Substructure
{
    public class StructPageBase : AWebPage
    {


        /// <summary>
        /// Oracle 数据库 Guid 绑定
        /// </summary>
        /// <param name="byteKey"></param>
        /// <returns></returns>
        public Guid ByteToGuid(object byteKey)
        {
            return new Guid((byte[])byteKey);
        }
        
        public string RegScript(string path)
        {
            return JKF.Substructure.ExtMethod.RegScript(this, path);
        }

        public string RegStyle(string path)
        {
            return JKF.Substructure.ExtMethod.RegStyle(this, path);
        }

        
        /// <summary>
        /// 绑定页面数据的时候截取字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public virtual string CutString(object obj, int length)
        {
            var str = obj.ToString();
            if (str.Length > length)
            {
                return str.Substring(0, length) + "...";
            }
            else
            {
                return str;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string FormatTag(object obj)
        {
            string tmpStr = "";
            if (obj != null)
            {
                tmpStr = obj.ToString();

                if (!string.IsNullOrEmpty(tmpStr))
                {
                    tmpStr = System.Web.HttpContext.Current.Server.HtmlEncode(tmpStr);
                }
            }

            return tmpStr;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string FormatTag(object obj, int length)
        {
            string tmpStr = "";
            if (obj != null)
            {
                tmpStr = obj.ToString();

                if (length != 0)
                {
                    tmpStr = bSubstring(tmpStr, length);
                }

                if (!string.IsNullOrEmpty(tmpStr))
                {
                    tmpStr = System.Web.HttpContext.Current.Server.HtmlEncode(tmpStr);
                }
            }

            return tmpStr;
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str">要截取的原串</param>
        /// <param name="length">截取长度</param>
        /// <returns>截取后的字符串</returns>
        public static string bSubstring(object str, int length)
        {
            string s = str.ToString();

            if (System.Text.Encoding.Default.GetByteCount(s) <= length + 1 || length < 1)
            {
                return s;
            }
            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s);
            int n = 0;
            int i = 0;
            for (; i < bytes.GetLength(0) && n < length; i++)
            {
                if (i % 2 == 0)
                {
                    n++;
                }
                else
                {
                    if (bytes[i] > 0)
                    {
                        n++;
                    }
                }
            }
            if (i % 2 == 1)
            {
                if (bytes[i] > 0)
                {
                    i = i - 1;
                }
                else
                {
                    i = i + 1;
                }
            }
            return System.Text.Encoding.Unicode.GetString(bytes, 0, i) + "…";
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            var a = Request.Url.OriginalString;
            var urllist = this.UrlList;
            if (urllist.Peek() == a)
            {

            }
            else
            {
                urllist.Push(a);
            }
            base.OnPreRenderComplete(e);
        }

        private string _keySessionUrlList = "keySessionUrlList";
        protected Stack<string> UrlList
        {
            get
            {
                if (JKF.Substructure.SessionManager.Exist(_keySessionUrlList))
                {
                    return JKF.Substructure.SessionManager.Get(_keySessionUrlList) as Stack<string>;
                }
                else
                {
                    var tmp = new Stack<string>();
                    tmp.Push(Request.Url.AbsolutePath);
                    JKF.Substructure.SessionManager.Set(_keySessionUrlList, tmp);
                    return tmp;
                }
            }
        }

        public virtual string LastUrl
        {
            get
            {
                return UrlList.ToList()[1];
            }
        }
        /// <summary>
        /// 预览权限，默认没有此权限
        /// </summary>
        public bool Preview = false;
        /// <summary>
        /// 添加权限，默认没有此权限
        /// </summary>
        public bool Add = false;
        /// <summary>
        /// 编辑权限，默认没有此权限
        /// </summary>
        public bool Edit = false;
        /// <summary>
        /// 删除权限，默认没有此权限
        /// </summary>
        public bool Delete = false;

        /// <summary>
        /// 调用Js方法关闭页面
        /// </summary>
        public void JavaScriptCloseWindow()
        {
            ClientScript.RegisterStartupScript(this.GetType(), "tishi", "alert('对不起，不允许非法访问，页面即将关闭！');closeWindow();", true);
        }

        /// <summary>
        /// 移除删除按钮
        /// </summary>
        public void JavaScriptRemoveDelete()
        {
            ClientScript.RegisterStartupScript(this.GetType(), "tishi", "removeDelete();", true);
        }

      

         
    }
}
